package com.blogsoho.taurus.controller.admin;

import com.blogsoho.taurus.constants.URIMapping;
import com.blogsoho.taurus.vm.ResponsePagerWrapper;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.AttachmentVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.time.LocalDate;

/**
 * Created by user on 2017/6/8.
 */
@Controller
@RequestMapping(URIMapping.ADMIN_ATTACHMENT)
public class AttachmentController extends BaseAdminController {

    @Value("${taurus.upload.path}")
    private String UPLOAD_PATH;

    @GetMapping("add")
    public String addAttachmentView() {
        return "admin/attachment_add";
    }

    @GetMapping("list")
    public String listAttachmentView() {
        return "admin/attachment_list";
    }

    @PostMapping("upload")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) throws IOException {

        String md5 = getMd5ByFile(file);
        ResponseWrapper<AttachmentVo> response = attachmentService.getAttachmentByMD5(md5);
        if (response.isSuccess()) {

            return new ResponseEntity<>(response.getData().getId(), HttpStatus.OK);

        } else {
            LocalDate date = LocalDate.now();
            String datePath = date.toString();
            String staticPath = "/static/" + datePath + "/";
            String dir = UPLOAD_PATH + staticPath;
            String originalFilename = file.getOriginalFilename();
            String path = dir + originalFilename;
            File f = new File(dir);
            if (!f.exists()) {
                f.mkdirs();
            }

            file.transferTo(new File(path));

            AttachmentVo attachment = new AttachmentVo();
            attachment.setPath(staticPath + originalFilename);
            attachment.setRealPath(path);
            attachment.setTitle(originalFilename);
            attachment.setSuffix(originalFilename.substring(originalFilename.indexOf(".")));
            attachment.setUserId("1");
            attachment.setMd5(md5);
            ResponseWrapper<?> responseWrapper = attachmentService.addAttachment(attachment);
            if (responseWrapper.isSuccess()) {
                return new ResponseEntity<>(responseWrapper.getData(), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
            }

        }
    }

    @PostMapping("list")
    @ResponseBody
    public ResponseWrapper<?> listAttachment(int start, int length, int draw) {
        ResponsePagerWrapper<?> response = new ResponsePagerWrapper<>();
        response = attachmentService.getAllAttachment(start, length);
        response.setDraw(draw);
        return response;
    }

    public String getMd5ByFile(MultipartFile file) {
        String value = null;
        try {
            byte[] byteBuffer = file.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
